package br.ufrn.dca.graficos;

import java.awt.BasicStroke;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CombinedDomainXYPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.SamplingXYLineRenderer;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;

public class Grafico extends ApplicationFrame {
    //Series Gráfico 1 - Níveis cm

    XYSeries series1 = new XYSeries("Nível T1");
    XYSeries series2 = new XYSeries("Nível T2");
    XYSeries series3 = new XYSeries("Nivel Estimado T1");
    XYSeries series4 = new XYSeries("Nivel Estimado T2");
    //Series Gráfico 2 - Erro cm
    XYSeries series5 = new XYSeries("Erro T1");
    XYSeries series6 = new XYSeries("Erro T2");
    //Sereies Grafico 3 - Volts
    XYSeries series7 = new XYSeries("Sinal Escrito");
    List<XYSeries> listaSeries = new ArrayList<XYSeries>(7);
    
    final XYSeriesCollection collection1 = new XYSeriesCollection();
    final XYSeriesCollection collection2 = new XYSeriesCollection();
    final XYSeriesCollection collection3 = new XYSeriesCollection();
    Color cinzaClaro = new Color(180, 180, 180);
    Color cinzaEscuro = new Color(70, 70, 70);

    /**
     * Constructs a new demonstration application.
     *
     * @param title  the frame title.
     */
    public Grafico(final String title) {
        super(title);

        listaSeries.add(series1);
        listaSeries.add(series2);
        listaSeries.add(series3);
        listaSeries.add(series4);
        listaSeries.add(series5);
//      listaSeries.add(series6);
//        listaSeries.add(series7);


        //Nivel T1, Nivel T2, Nivel Estimado T1, Nivel Estimado T2
        collection1.addSeries(series1); //Sinal Lido - Nível T1
        collection1.addSeries(series2); //Sinal Lido - Nível T2
        collection1.addSeries(series3); // Nivel Estimado T1
        collection1.addSeries(series4); //Nivel Estimado T2

        // Erro 
        collection2.addSeries(series5); //Erro entre T1 e est-T1
        collection2.addSeries(series6); //Erro entre T2 e est-T2


        //Sinal Escrito
        collection3.addSeries(series7); //Sinal Escrito

        final JFreeChart chart = createCombinedChart();
        chart.setBackgroundPaint(new Color(77, 77, 77));
        final ChartPanel panel = new ChartPanel(chart, true, true, true, false, true);
        panel.setPreferredSize(new java.awt.Dimension(750, 400));
        setContentPane(panel);
    }

    /**
     * Creates a combined chart.
     *
     * @return the combined chart.
     */
    private JFreeChart createCombinedChart() {
        // create subplot 1...
        final XYDataset data1 = collection1;
        final XYDataset data2 = collection2;
        final XYDataset data3 = collection3;

        final XYItemRenderer renderer1 = new StandardXYItemRenderer();
        final XYItemRenderer renderer2 = new StandardXYItemRenderer();
        final XYItemRenderer renderer3 = new SamplingXYLineRenderer();
        renderer1.setSeriesStroke(0, new BasicStroke(2.0f));
        //renderer1.setSeriesStroke(1, new BasicStroke(2.0f));
        //renderer1.setSeriesStroke(2, new BasicStroke(2.0f));
        renderer2.setSeriesStroke(0, new BasicStroke(2.0f));
        renderer3.setSeriesStroke(0, new BasicStroke(2.0f));
        


        final NumberAxis rangeAxis1 = new NumberAxis("Nível (cm)");
        final NumberAxis rangeAxis2 = new NumberAxis("Erro (cm)");
        final NumberAxis rangeAxis3 = new NumberAxis("Volts");
        
        rangeAxis1.setAxisLinePaint(cinzaClaro);
        rangeAxis1.setLabelPaint(cinzaClaro);
        rangeAxis1.setTickLabelPaint(cinzaClaro);
        
        rangeAxis3.setAxisLinePaint(cinzaClaro);
        rangeAxis3.setLabelPaint(cinzaClaro);
        rangeAxis3.setTickLabelPaint(cinzaClaro);
        
        rangeAxis2.setAxisLinePaint(cinzaClaro);
        rangeAxis2.setLabelPaint(cinzaClaro);
        rangeAxis2.setTickLabelPaint(cinzaClaro);

        final XYPlot subplot1 = new XYPlot(data1, null, rangeAxis1, renderer1);
        subplot1.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        subplot1.setBackgroundPaint(Color.darkGray);
        
        final XYPlot subplot2 = new XYPlot(data2, null, rangeAxis2, renderer2);
        subplot2.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        subplot2.setBackgroundPaint(Color.darkGray);

        final XYPlot subplot3 = new XYPlot(data3, null, rangeAxis3, renderer3);
        subplot3.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
        subplot3.setBackgroundPaint(Color.darkGray);


        /*
        final XYTextAnnotation annotation = new XYTextAnnotation("Hello!", 50.0, 10000.0);
        annotation.setFont(new Font("SansSerif", Font.PLAIN, 9));
        annotation.setRotationAngle(Math.PI / 4.0);
        subplot1.addAnnotation(annotation);
         */

        // parent plot...
        final NumberAxis tempoAxis = new NumberAxis("Tempo (s)");
        tempoAxis.setAxisLinePaint(cinzaClaro);
        tempoAxis.setLabelPaint(cinzaClaro);
        tempoAxis.setTickLabelPaint(cinzaClaro);
        final CombinedDomainXYPlot plot = new CombinedDomainXYPlot(tempoAxis);
        //plot.setRangeMinorGridlineStroke(new BasicStroke(5.0f));
        //plot.setDomainGridlineStroke(new BasicStroke(5.0f));

        plot.setGap(10.0);
        // add the subplots...
        plot.add(subplot1, 2);
        plot.add(subplot2, 1);
        plot.add(subplot3, 1);
        plot.setOrientation(PlotOrientation.VERTICAL);

        // return a new chart containing the overlaid plot...
        JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
        //chart.getTitle().setPaint(cinzaClaro);
        chart.getLegend().setItemPaint(cinzaClaro);
        chart.getLegend().setBackgroundPaint(cinzaEscuro);
        chart.getLegend().setBorder(new BlockBorder(new Color(50, 50, 50)));
        return chart;
    }

    public void adicionarValores(double tempo, double sinalLido1, double sinalLido2, double sinalEstimado1, double sinalEstimado2, double sinalEscrito, double erro1, double erro2) {
        series1.add(tempo, sinalLido1);
        series2.add(tempo, sinalLido2);
        series3.add(tempo, sinalEstimado1);
        series4.add(tempo, sinalEstimado2);
        series5.add(tempo, erro1);
        series6.add(tempo, erro2);
        series7.add(tempo, sinalEscrito);
    }

    public void atualizarSeries(List<Boolean> lista) {
        collection1.removeAllSeries();
        for (int i = 0; i < lista.size(); i++) {
            if (lista.get(i)) {
                if (i < 4) {
                    collection1.addSeries(listaSeries.get(i));
                }
            }
        }
        collection2.removeAllSeries();
        for (int i = 0; i < lista.size(); i++) {
            if (lista.get(i)) {
                if (i < 2) {
                    collection2.addSeries(listaSeries.get(i));
                }
            }
        }
        collection3.removeAllSeries();
        for (int i = 0; i < lista.size(); i++) {
            if (lista.get(i)) {
                if (i < 1) {
                    collection3.addSeries(listaSeries.get(i));
                }
            }
        }
    }
    // ****************************************************************************
    // * JFREECHART DEVELOPER GUIDE                                               *
    // * The JFreeChart Developer Guide, written by David Gilbert, is available   *
    // * to purchase from Object Refinery Limited:                                *
    // *                                                                          *
    // * http://www.object-refinery.com/jfreechart/guide.html                     *
    // *                                                                          *
    // * Sales are used to provide funding for the JFreeChart project - please    *
    // * support us so that we can continue developing free software.             *
    // ****************************************************************************
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
//    public static void main(final String[] args) {
//
//        final Grafico demo = new Grafico("GrÃ¡ficos");
//        demo.pack();
//        RefineryUtilities.centerFrameOnScreen(demo);
//        demo.setVisible(true);
//
//    }
}
